#include "hash_table.h"
#include "spisok.h"
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h> 
#include <ctype.h>
#include <crtdbg.h>
#include <ctype.h>
#include <locale.h>
#include <windows.h>
#pragma warning(disable:4996)
void clear_end_of_word(unsigned char* word)
{
        int i = 0;
        int pos = 0;
        do
        {
                if( !ispunct(word[i]) )
                {
                        pos = i;
                }
                i++;            
        }while(word[i] != '\0');
        word[pos + 1] = '\0';
}

int words(FILE *file, unsigned char* h)
{
      
       
        int i=0;
        int a=1;
		int ch;
         i = 0; 
		 memset(h, 0, 32*sizeof(char));
        while(1)
        {
                ch = fgetc(file);

                if ( ch == EOF )
                        return 0;

                if ( !isspace(ch) && !ispunct(ch))
                {
                        h[i] = ch;
                        i++;
                        break;
                }
        }

        while(1)
        {
                ch = fgetc(file);
                if ( ch != EOF && !isspace(ch))
                {       
                        h[i] = ch;
                        i++;
                }
                else 
                {
                        clear_end_of_word(h);
                        return 1;
                }
        }
        clear_end_of_word(h);
        return 1;
        
                        
                
        
}


struct Student
{
	int id;
	double avr;
	char * name;
};

struct Student * create_student(int id, double avr, char * name)
{
	struct Student * p = malloc(sizeof(struct Student));
	size_t len;

	p->id = id;
	p->avr = avr;

	len = strlen(name);
	p->name = malloc(len + 1);

	strcpy(p->name, name);

	return p;
}

void delete_student(Pointer _p)
{
	

	
	
		free(_p);
	
}

int main()
{
		
         HashTable ht;
                struct Student * p;
                struct Student * t;
                struct Student* s;
                FILE* fp;
				FILE* fp1;
				char k[32];
				setlocale(LC_ALL, "Russian_Russia");	
                if((fp=fopen("text.txt","r"))==NULL) 
                {
					printf("Error!\n");

                
                }
				if((fp1=fopen("text1.txt","w"))==NULL) 
                {
                printf("Error!\n");

                
                }

				setlocale(LC_ALL, "Russian_Russia");

                //st=student("Vasya,Petya","Groupa123");
                
                ht_init(&ht, 10000, 0, delete_student);
              
 do{
                if( !words(fp, &k) )
                {
                        break;
                }

                if( !ht_has(&ht, &k) )
                {
                        ht_set(&ht, &k, (Pointer)1);     
                }
                else
                {
                        int count2 = (int)(ht_get(&ht, k));
                        count2++;
                        ht_set(&ht, k, (Pointer)count2);
                        
                }
        }while(1);
		 write_to_file(&ht, fp1);
        //ht_traverse(&ht, &log_to_consol);
        ht_destroy(&ht);

        fclose(fp);
        fclose(fp1);
       
        system("PAUSE");
        return 0;
	
                /*p = create_student(22, 4.6, "Petya");
                t= create_student(21, 4.6, "Ivan");
                ht_set(&ht, "k", p);
                s=ht_get(&ht,"k");
                //ht_delete(&ht,"k");
                ht_set(&ht, "l", t);
                ht_destroy(&ht);*/

                system("pause");
        return 0;
}

